package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import helper.StringHelper;
	
	import roleEditor.common.ScreenMessage;
	import roleEditor.model.RoleModel;
	import roleEditor.view.ItemSetView;
	import roleEditor.view.PreviewView;
	import roleEditor.view.RoleBaseInfoView;
	import roleEditor.view.TopMenuView;
	
	[SWF(width = "900",height = "600",frameRate = "25")]
	public class Main extends Sprite
	{
		private var _model:RoleModel;
		private var _topMenuView:TopMenuView;
		private var _itemSetView:ItemSetView;
		private var _roleBaseInfoView:RoleBaseInfoView;
		private var _rolePreviewView:PreviewView;
		
		public function Main()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			setView();
			setEvent();
		}
		
		private function setView():void
		{
			_model=new RoleModel();
			
			_topMenuView=new TopMenuView();
			addChild(_topMenuView);
			
			_itemSetView=new ItemSetView();
			_itemSetView.y=_topMenuView.height+10;
			_itemSetView.itemBase.gotoAndStop(2);
			addChild(_itemSetView);
			
			setBaseView();
		}
		
		private function setEvent():void
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			_itemSetView.itemBase.addEventListener(MouseEvent.CLICK, onItemClick);
			_itemSetView.itemAction.addEventListener(MouseEvent.CLICK, onItemClick);
			_itemSetView.itemPreview.addEventListener(MouseEvent.CLICK, onItemClick);
		}
		
		private function onAddedToStage(evt:Event):void
		{
			ScreenMessage.Instance.register(stage);
		}
		
		private function onItemClick(evt:MouseEvent):void
		{
			if(_roleBaseInfoView)
			{//保存基本信息
				_roleBaseInfoView.saveBaseInfo();
			}
			
			if(!checkInfo()) return;
			
			resetView();
			
			_itemSetView.resetItem();
			switch(evt.target)
			{
				case _itemSetView.itemBase://基本信息
					_itemSetView.itemBase.mouseEnabled=false;
					_itemSetView.itemBase.gotoAndStop(2);
					setBaseView();
					break;
				case _itemSetView.itemPreview://角色预览
					_itemSetView.itemPreview.mouseEnabled=false;
					_itemSetView.itemPreview.gotoAndStop(2);
					setPreviewView();
					break;
			}
		}
		
		/**
		 *重置所有选项卡视图 
		 */		
		private function resetView():void
		{
			if(_roleBaseInfoView)
			{
				if(_roleBaseInfoView.parent) _roleBaseInfoView.parent.removeChild(_roleBaseInfoView);
				_roleBaseInfoView.dispose();
			}
			_roleBaseInfoView=null;
			
			if(_rolePreviewView)
			{
				if(_rolePreviewView.parent) _rolePreviewView.parent.removeChild(_rolePreviewView);
				_rolePreviewView.dispose();
			}
			_rolePreviewView=null;
		}
		
		
		/**
		 *检查各项设置信息 
		 */		
		private function checkInfo():Boolean
		{
			if(StringHelper.IsNullOrEmpty(_model.roleName) || StringHelper.IsNullOrEmpty(_model.configPath))
			{
				ScreenMessage.Instance.msg="角色名称或配置文件未填写!";
				return false;
			}
			
			return true;
		}
		
		/**
		 *设置基本信息视图 
		 */		
		private function setBaseView():void
		{
			if(!_roleBaseInfoView)
			{
				_roleBaseInfoView=new RoleBaseInfoView(_model);
				_roleBaseInfoView.x=15;
				_roleBaseInfoView.y=_topMenuView.height+_itemSetView.height+20;
				addChild(_roleBaseInfoView);
			}
		}
		
		/**
		 *设置预览视图 
		 */		
		private function setPreviewView():void
		{
			if(!_rolePreviewView)
			{
				_rolePreviewView=new PreviewView(_model);
				_rolePreviewView.x=15;
				_rolePreviewView.y=_topMenuView.height+_itemSetView.height+20;
				addChild(_rolePreviewView);
			}
		}		
		
		
		public function dispose():void
		{
			_itemSetView.itemBase.removeEventListener(MouseEvent.CLICK, onItemClick);
			_itemSetView.itemAction.removeEventListener(MouseEvent.CLICK, onItemClick);
			_itemSetView.itemPreview.removeEventListener(MouseEvent.CLICK, onItemClick);
		}
	}
}